// pages/Deposit/Deposit.js
const app = getApp();
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');

Page({


  data: {
    money: null,
    visible: false,
    showModalStatus: false,
    height: '',
    tool: false,
    load: false,
    isShow: false,
    tops: Number(getApp().globalData.statusBarHeight + 48),
    isIPX: app.globalData.isIPX,
    cash_apply:false
  },
  money: function (e) {
    app.txmoney = e.detail.value;
    let moneys = e.detail.value;
    moneys = parseInt(moneys).toFixed(2)
    this.setData({
      money: e.detail.value,
      moneys: moneys
    })
  },
  // 关闭模态框

  hideTool() {
    this.setData({
      tool: false
    })
  },
  onAllMoney(e) {
    var _this = this
    _this.setData({
      money: e.currentTarget.id
    })
  },
  //申请提现
  cashForward(data) {
    let _this = this
    http.postRequest('v1/cashForward', data,
      function (res) {
        _this.hideModal()

        wx.redirectTo({
          url: '/pages/onNewPages/RechSucess/RechSucess?money=' + res.data.money + '&msg=' + res.data.reminder + '&&agent=1',
        })
      },
      function (err) {
        _this.toastDialog.showDialog(err.msg)
        _this.hideModal()
      })
  },
  //点击赋值
  Choose(e) {
    let index = e.currentTarget.dataset.id;
    let bank = this.data.bankList[index];
    this.setData({
      cash_apply: bank
    })
    this.hideModal();
  },
  //继续提现
  Cash(e) {
    let jump = this.data.jump;
    let money = this.data.money;
    var _this = this
    // 微信提现 
    switch (jump) {
      case "1":
        let cash_id = _this.data.putForward.cash_apply.id;
        this.cashForward({
          cash_type: 1,
          cash_money: money,
          cash_id: cash_id
        })
        break;
      case "2":
        var cash_id = this.data.cash_apply.id;
        this.cashForward({
          cash_type: 2,
          cash_money: money,
          cash_id: cash_id
        })
        break;
    }

    //银行卡提现
  },
  //微信提现
  putForwards(data) {
    let _this = this
    http.getRequest('v1/putForward', data,
      function (res) {
        console.log(res)
        _this.setData({
          is_cad: 0
        })
        let putForward = res.data;
        if (data.cash_type != 1) {
          if (res.data.cash_apply) {
            putForward.cash_apply.bank_card = putForward.cash_apply.bank_card.substring(putForward.cash_apply.bank_card.length - 4)
          }
        }
        putForward.money = Number(putForward.money);
        let min_money = Number(putForward.verify_info.min_money);
        let max_money = Number(putForward.verify_info.max_money);
        _this.setData({
          min_money: min_money,
          max_money: max_money,
          putForward: putForward,
          cash_apply: res.data.cash_apply
        })
      },
      function (err) {
        if (err.code == 210) {
          _this.setData({
            is_cad: 1
          })
        }
      })
  },
  //银行卡列表
  getBankOwner(data) {

    let _this = this
    _this.toasts.showToast({
      type: 'fail',
      title: '加载中',
      duration: 7000,
    })
    http.getRequest('v1/bank', data,
      function (res) {
        _this.toasts.hideToast({})
        if (res.data.list.length != 0) {

          for (var p = 0; p < res.data.list.length; p++) {
            let len = res.data.list[p].length;
            res.data.list[p].num = res.data.list[p].bank_card.slice(-4)

          }
          _this.setData({
            bankList: res.data.list,

          })
        }

      },
      function (err) {

      })

  },
  //添加新卡
  toCard() {
    let member = wx.getStorageSync('member');
    if (wx.getStorageSync('renzheng') != '1') {
      this.Modal.showModal()

    } else {
      wx.navigateTo({
        url: '/pages/onNewPages/CardInfo/CardInfo?realname=' + member.member_info.realname,
      })
    }
    // 0未认证
    // switch (is_cards) {
    //   case 0:
    //     // this.Modal.showModal();
    //     break;
    //   case 1:
    //     var realname = content.data.content.userinfo.member_info.realname;
    //     let id_card = content.data.content.userinfo.member_info.id_card;
    //     wx.navigateTo({
    //       url: '../CardInfo/CardInfo?realname=' + realname + '&id_card=' + id_card,
    //     });
    //     break;
    //     //添加银行卡跳转路径  1提现  
    //     app.card_type = 1;
    // }

  },
  //去认证

  _confirmEventFirst: function () {

    wx.navigateTo({
      url: '/pages/onNewPages/Certification/Certification?',
    })

    this.Modal.hideModal();

  },
  _cancelEvent: function () {
    this.Modal.hideModal();
  },
  //返回
  back() {
    let agent = this.data.agent;
    if (agent) {
      wx.navigateBackMiniProgram({})
    } else {
      wx.navigateBack({
        delta: 1,
      })
    }
  },
  //提现失败
  toDespositfalse: function () {
    let money = this.data.money;
    let min_money = Number(this.data.putForward.verify_info.min_money);
    let min_tips = this.data.putForward.verify_info.min_money_tips;
    let max_money = Number(this.data.putForward.verify_info.max_money);
    let max_tips = this.data.putForward.verify_info.max_money_tips;
    this.setData({
      min_money: min_money,
      max_money: max_money
    })
    if (money < min_money) {
      this.toastDialog.showDialog(min_tips)

    } else if (money > max_money) {
      this.toastDialog.showDialog(max_tips)

    }
  },
  //显示对话框  
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  //服务费以及服务费计算
  Sum() {
    let money = this.data.money;
    money = parseInt(money).toFixed(2)
    let sum_money = this.data.putForward.money;
    // 计算服务费方式
    let service_type = this.data.putForward.service_charge.rate_type;
    let service_money = this.data.putForward.service_charge.service_rate;
    let sum = '';
    let service = '';
    this.setData({
      service_type: service_type
    })
    // 1固定   2百分比
    switch (service_type) {
      case 0:
        this.setData({
          sum: Number(money),
          service: '免费',
        })
        break;
      case 1:
        service = service_money;
        sum = (Number(money) - Number(service_money)).toFixed(2);
        let sums = Number(money) + Number(service_money);
        if (sums > Number(sum_money) || sums == Number(sum_money)) {
          this.setData({
            sum: sum
          })
        } else {
          this.setData({
            sum: money
          })
        }
        this.setData({
          service: service,
        })
        break;
      case 2:
        service = (Number(money) * this.data.putForward.service_charge.service_rate).toFixed(2);
        sum = (Number(money) - Number(service)).toFixed(2);
        var sums = Number(money) + Number(service);
        if (sums > Number(sum_money) || sums == Number(sum_money)) {
          this.setData({
            sum: sum
          })
        } else {
          this.setData({
            sum: money
          })
        }
        this.setData({
          service: service,
        })
        break;
    }
  },

  onLoad: function (options) {
    var _this = this
    let jump = options.jump;
    let member = wx.getStorageSync('member')
    _this.setData({
      jump: jump,
      name: member.member_oauth_info.nickname
    })
  },


  onReady: function () {
    this.Modal = this.selectComponent("#modal");
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },


  onShow: function () {
    let jump = this.data.jump
    this.putForwards({
      cash_type: Number(jump)
    })
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  _onShowModal: function (e) {
    this.setData({
      tool: true
    })
    this.Sum()
    // this.Modal.showModal();
    // let agent = this.data.agent;
    // let jump = this.data.jump;
    // let content = wx.getStorageSync('content')
    // if (agent && !content) {
    //   this.shows('您的账号未登录！')
    //   setTimeout(()=>{
    //     wx.redirectTo({
    //       url: '../Accredit/Accredit?agent=' + agent + '&&jump=' + jump,
    //     })
    //   },1500)

    // } else {

    // }
  },
  // 选择卡
  showModal() {
    this.setData({
      showModalStatus: true
    })
    this.getBankOwner('')
  }
})